package com.twc.ftp.filters;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by twc on 13:54 2024/4/26.
 */
@Component
public class UrlFilter implements Filter {

    @Value("${server.adminPassword}")
    private String adminPassword;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化管理员密码："+adminPassword);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String adminPass = (String) req.getSession().getAttribute("adminPass");
        if ((!(adminPassword.equals(adminPass)))) {
            response.sendRedirect("/page/login");
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
    }
}
